//
//  LQTextAttarchment.swift
//  emotIcon
//
//  Created by 半缘君 on 15/12/16.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

class LQTextAttachment: NSTextAttachment {
    
    private var emotIcon:Emoticon
    init(em:Emoticon) {
        emotIcon = em
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置字体//根据当前的font计算图片的宽高
    func imageText (font:UIFont) -> NSMutableAttributedString {
        
        // 图片
        let image = UIImage(contentsOfFile: emotIcon.imagePath!)
        
       self.image = image
        
        let lineHeight = font.lineHeight
        
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        
        //设置字体//保证光标之后输入的内容一样大小
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return imageText
    }
}
